/*!
 * 下拉菜单控件.
 * ecp.qzz.ui.drop.popmenu.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.pwindow"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.drop");
	/**
	 * 下拉菜单控件.
	 */
	qzz.ui.drop.popmenu = qzz.Class(qzz.ui.drop.popwindow, {
		/**
		 * 创建.
		 */
	    _create:function() {
		    this.base();
		    this.className = "PopMenu";
		    this.logInfo("popMenu.create");
		    this._popList = [];
		    this._popCount = 0;
	    },
	    /**
	     * 渲染.
	     */
	    _render:function(){
	    	this.base();
	    	this.logInfo("popMenu.render");
	    	this._mTab = this.createElement("table");
		    this.appendChild(this._mTab);
	    	this.refresh();
	    },
	    /**
	     * 结束刷新.
	     */
	    endUpdate:function() {
	    	this._update = true;
	    	this.refresh();
	    },
	    /**
	     * 获取菜单项.
	     * @param mName 名称
	     */
	    getMenu:function(mName) {
	    	for (var i = 0; i < this._popCount; i++) {
	            if (this._popList[i]["name"] == mName) {
	                return this._popList[i];
	            }
	        }
	        return null;
	    },
	    /**
	     * 添加菜单项.
	     * @param mName 名称
	     * @param mCaption 显示名称
	     * @param ent 事件
	     * @param shutCut 快捷键
	     * @param enable 是否可用
	     * @param mIcon 显示图标
	     */
	    addMenu:function(mName, mCaption, ent, shutCut, enable, mIcon) {
	    	var mObj = {};
	        mObj["name"] = mName;
	        mObj["caption"] = mCaption;
	        mObj["icon"] = mIcon;
	        mObj["event"] = ent;
	        mObj["enable"] = enable;
	        mObj["shutCut"] = shutCut;
	        this._popList[this._popCount++] = mObj;
	    	this.refresh();
	        return mObj;
	    },
	    /**
	     * 插入菜单项.
	     * @param index 序号
	     * @param mName 名称
	     * @param mCaption 显示名称
	     * @param ent 事件
	     * @param shutCut 快捷键
	     * @param enable 是否可用
	     * @param mIcon 显示图标
	     */
	    insertMenu:function(index, mName, mCaption, ent, shutCut, enable, mIcon) {
	    	var mObj = {};
	        mObj["name"] = mName;
	        mObj["caption"] = mCaption;
	        mObj["icon"] = mIcon;
	        mObj["event"] = ent;
	        mObj["enable"] = enable;
	        mObj["shutCut"] = shutCut;
	        if (index >= 0 && index <= this._popCount) {
	            for (var i = this._popCount - 1; i >= index; i--) {
	            	this._popList[i + 1] = this._popList[i];
	            }
	            this._popList[index] = mObj;
	            this._popCount++;
	        }
	    	this.refresh();
	        return mObj;
	    },
	    /**
	     * 刷新菜单项.
	     * @param mName 名称
	     * @param mCaption 显示名称
	     * @param ent 事件
	     * @param shutCut 快捷键
	     * @param enable 是否可用
	     * @param mIcon 显示图标
	     */
	    updateMenu:function(mName, mCaption, ent, shutCut, enable, mIcon){
	    	var mObj = this.getMenu(mName);
	    	if(mObj != null) {
	            mObj["caption"] = mCaption;
	            mObj["icon"] = mIcon;
	            mObj["event"] = ent;
	            mObj["enable"] = enable;
	            mObj["shutCut"] = shutCut;
	    	}
	    	this.refresh();
	    	return mObj;
	    },
	    /**
	     * 删除菜单项.
	     * @param mName 名称
	     */
	    delMenu:function(mName) {
	    	var hasFound = false;
	        var index = 0;
	        //循环查找项目.
	        for (var i = 0; i < this._popCount; i++) {
	            if (this._popList[i]["name"] == mName) {
	                hasFound = true;
	                index = i;
	                break;
	            }
	        }
	        if (hasFound) {
	        	//删除处理
	            var mObj = this._popList[index];
	            if (mObj != null) {
	                mObj["name"] = undefined;
	                mObj["caption"] = undefined;
	                mObj["icon"] = undefined;
	                mObj["event"] = undefined;
	                mObj["enable"] = undefined;
	                mObj["shutCut"] = undefined;
	            }
	            for (var i = index; i < this._popCount - 1; i++) {
	            	this._popList[i] = this._popList[i + 1];
	            }
	            this._popCount--;
	        }
	        this.refresh();
	    },
	    /**
	     * 清除菜单项
	     * @name clearMenu
	     */
	    clearMenu:function() {
	    	this._popCount = 0;
	    },
	    /**
	     * 设置可用状态.
	     * @param mName 名称
	     * @param bv 状态
	     */
	    setEnable:function(mName, bv) {
	    	var mObj = this.getMenu(mName);
	    	if(mObj != null) {
		        mObj["enable"] = bv;
		        if(!bv) {
		        	//除去事件及颜色
		        	var mrow = mObj["row"]
		            mrow.onclick = null;
		            for(var i = 0; i < mrow.cells.length; i++ ) {
		                mrow.cells[i].style.color = "#AAAAAA";
		            }
		        } else {
		        	var mrow = mObj["row"];
		        	var _this = this;	
		        	//添加事件
		        	mrow.onclick = function(){
                    	if (typeof this.event == "function") {
            	            this.event();
            	        }
            	        _this.hide();
                    };
                    //设置颜色
		        	for(var i = 0; i < mrow.cells.length; i++ ) {
		                mrow.cells[i].style.scolor = "#000000";
		            }
		        }
	        }
	    	this.refresh();
	    },	 
	    /**
	     * 获取菜单项目数量.
	     * @return 返回数量
	     */
	    getMenuCount:function() {
	        return this._popCount
	    },
	    refreash:function() {
	    	this.refresh();
	    },
	    /**
	     * 刷新.
	     */
	    refresh:function(){
	    	if(!this._update) return false;
	    	for (var i = this._mTab.rows.length - 1; i >= 0; i--) {
	            this._mTab.deleteRow(i);
	        }
	    	var _this = this;
	        this.setStyle(this._mTab, "popMain");
	        var hasAdd = false;
	        var maxLen = 4;
	        var maxSCLen = 1;
	        var hasIcon = false;
	        var rowHeight = 20;
	        _height = 0;
	        //循环刷新菜单列表
	        for (var i = 0; i < this._popCount; i++) {
	            var mObj = this._popList[i];            
	            if (mObj != null) {
	                var row = this._mTab.insertRow(i);
	                row.oncontextmenu = function() {
	                	return false;
	                };
	                if (mObj["caption"] == "-") {
	                	//分隔线
	                    col = row.insertCell(0);
	                    if (mObj["icon"] != null) {
	                        mIcon = myCreateElement("img");
	                        mIcon.src = mObj["icon"];
	                        col.appendChild(mIcon);
	                    }
	                    this._setHtml(col, "<hr class='"+this._cssPrefix+"lineColor'>", false);
	                    col.colSpan = 3;
	                    col.style.height = rowHeight + "px";
	                    _height += rowHeight + 2;
	                } else {
	                	//实际菜单项目.
	                    this.setStyle(row, "popCaption");
	                    //添加事件
	                    var enb = mObj["enable"] == null || mObj["enable"];
	                    row.event = mObj["event"];
	                    if (enb) {
	                        row.onclick = function(){
	                        	if (this.event != undefined) {
	                                this.event();
	                            }
	                            _this.hide();
	                        };
	                    } else {
	                        row.onclick = null;
	                        row.onmouseover = null;
	                    }
	                    //添加图标
	                    col = row.insertCell(0);
	                    this.setStyle(col, "popIcon");
	                    if (mObj["icon"] != null) {
	                        mIcon = myCreateElement("img");
	                        mIcon.src = mObj["icon"];
	                        col.appendChild(mIcon);
	                        hasIcon = true;
	                    }
	                    //字体置灰
	                    if(enb) {
	                        col.style.color = "#000000";
	                    } else {
	                    	col.style.color = "#AAAAAA";
	                    }	                        
	                    col = row.insertCell(1);
	                    //this.setStyle(col, "popCaption");
	                    this._setHtml(col, mObj["caption"]);
	                    var sl = this.strLen(mObj["caption"]);
	                    if(sl > maxLen) {
	                    	maxLen = sl;
	                    }
	                    col.style.height = rowHeight + "px";
	                    if(enb) {
	                        col.style.color = "#000000";
	                    } else {
	                    	col.style.color = "#AAAAAA";
	                    }	
	                    //显示快捷键
	                    col = row.insertCell(2);
	                    //	this.setStyle(col, "popCaption");
	                    if(mObj["shutCut"] != null) {
	                    	this._setHtml(col, mObj["shutCut"]);
	                        sl = this.strLen(mObj["shutCut"]);
	                    }
	                    if(sl > maxSCLen) {
	                    	maxSCLen = sl;
	                    }
	                    col.style.width = "50px";
	                    if(enb) {
	                        col.style.color = "#000000";
	                    } else {
	                    	col.style.color = "#AAAAAA";
	                    }	
	                    _height += rowHeight + 2;
	                    sl = null;
	                }
	                mObj["row"] = row;
	                hasAdd = true;
	                row = null;
	            }
	            mObj = null;
	        } // for
	        if(this._popCount > 0) {
	        	//计算宽高
	        	var cwidth = maxLen * 10;
	        	var swidth = maxSCLen * 10;
	        	if(this._mTab.rows[0].cells.length > 1) {
	        	    this._mTab.rows[0].cells[1].style.width = cwidth + "px";
	        	    this._mTab.rows[0].cells[2].style.width = swidth + "px";
	        	} else {
	        		if(this._mTab.rows[1].cells.length > 1) {
	            	    this._mTab.rows[1].cells[1].style.width = cwidth + "px";
	            	    this._mTab.rows[1].cells[2].style.width = swidth + "px";
	        		}
	        	}
	        	var dwidth = cwidth + swidth;
	        	if(hasIcon) {
	        		dwidth += 20;
	        	}        	
	        	this.setWidth(dwidth);
	        	this.setHeight(_height);
	        	dwidth = null;
	        	cwidth = null;
	        	swidth = null;
	        }
	        hasAdd = null;
	        maxLen = null;
	        maxSCLen = null;
	        hasIcon = null;
	    }
	});
	_bindRenderFunc("qzzpopmenu", qzz.ui.drop.popmenu);
	return qzz.ui.drop.popmenu;
});